package com.itheima.quick_demo;

import com.itheima.domain.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Demo {

  private  SqlSession sqlSession;
    @Before
    public void before() throws IOException {
        //1.加载MyBatisConfig.xml核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        //2.创建SqlSessionFactoryBuilder
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        //3.使用SqlSessionFactoryBuilder创建SqlSessionFactory
        SqlSessionFactory factory = builder.build(is);

        //4.使用factory创建SqlSession
        sqlSession = factory.openSession();
    }
    @After
    public void after() throws IOException {
        //7.释放资源
        sqlSession.close();
    }

    @Test
   public void  testDindAll() throws IOException {
       //5.调用sqlSession方法查询
        List<Student> list = sqlSession.selectList("StudentMapper.selectAll");

       //6.处理结果
       for (Student stu : list) {
           System.out.println(stu);
       }
   }






}
